package com.kma.ncpractice2013.model;

import java.io.*;

/**
 * User: Illyas
 * Date: 11/14/13
 * Time: 2:53 AM
 */

public class Commission implements Serializable
{
    private static int value;
    private static final int defaultValue = 10;
    private static final int minValue = 3;
    private static final int maxValue = 17;

    private static void serialize()
    {
        try
        {
            FileOutputStream file = new FileOutputStream("commission.ser");
            ObjectOutputStream stream = new ObjectOutputStream(file);
            stream.writeObject(value);
            stream.close();
            file.close();
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    private static void deserialize()
    {
        try
        {
            FileInputStream file = new FileInputStream("commission.ser");
            ObjectInputStream stream = new ObjectInputStream(file);
            value = (Integer)stream.readObject();
            stream.close();
            file.close();
        }
        catch (Exception e)
        {
            value = defaultValue;
        }
    }

    static
    {
        deserialize();
    }

    public static int getMinValue()
    {
        return minValue;
    }

    public static int getMaxValue()
    {
        return maxValue;
    }

    public static int getValue()
    {
        return value;
    }

    public static boolean trySetValue(Integer value)
    {
        boolean ok = false;
        if (value != null && value >= getMinValue() && value <= getMaxValue())
        {
            Commission.value = value;
            ok = true;
            serialize();
        }
        return ok;
    }
}
